const util = require('../../utils/util.js')
const app = getApp()
Page({
  data: {
    /**列表数据 */
    feed: [],
    /**长度 */
    feedLen: 0,
  },
  onLoad: function() {
    this.getData()
  },
  getData: function() {
    const data = util.getData2();
    const feedData = data.data
    this.setData({
      feed: feedData,
      feedLen: feedData.length
    })
  },
  // 事件处理
  /**上拉刷新 */
  handleUpper: function() {
    wx.showNavigationBarLoading()
    this.handleRefresh()
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 2000)
  },
  /**下拉加载数据**/
  handleLower: function() {
    wx.showNavigationBarLoading()
    setTimeout(() => {
      wx.hideNavigationBarLoading({
        success: (res) => {},
      })
      this.handleNextLoad()
    }, 1000)
  },
  /**刷新函数 */
  handleRefresh: function() {
    wx.showToast({
      title: '刷新中',
      icon: 'loading',
      duration: 2000
    })
    const feed = util.getData2();
    const feedData = feed.data
    this.setData({
      feed: feedData,
      feedLen: feedData.length
    })
    setTimeout(() => {
      wx.showToast({
        title: '刷新成功',
        icon: 'success',
        duration: 1000
      })
    }, 2000)
  },
  /**下拉加载更多数据 */
  handleNextLoad: function(){
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 2000
    })
    const next = util.getNext();
    const nextData = next.data;
    this.setData({
      feed: this.data.feed.concat(nextData),
      feedLen: this.data.feedLen + nextData.length
    });
    setTimeout(function(){
      wx.showToast({
        title: '加载成功',
        icon: 'success',
        duration: 2000
      })
    },2000)
  },
  /**跳转到问题页 */
  gotoQue(e) {
    const question = e.currentTarget.dataset.question || ''
    wx.navigateTo({
      url: `../question/question?question=${question}`,
    })
  },
  /**跳转到回答页面 */
  gotoAnswer() {
  const question = e.currentTarget.dataset.question || ''
  const content = e.currentTarget.dataset.content || ''
  wx.navigateTo({
    url: `../answer/answer?question=${question}&content=${content}`,
  })
  }
})